from django.test import TestCase
from django.test.client import Client

from mdcssport.mdcsapp.models import PlayerUserProfile

"""
Testira ponistavanje meceva
"""
class UndoMatchTestCase(TestCase):
    
    def setUp(self):
        pass

    def runTest(self):
        self.runTestNotExist()
        self.runTestCantUndo()
        self.runTestChessUndo()
        self.runTestTableFootballUndo()

    def runTestNotExist(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.get("/mdcs/match/undo/100/")
        self.failUnlessEqual(response.status_code, 404)
        
    def runTestCantUndo(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.get("/mdcs/match/undo/1/")
        self.failIf(response == None)
        self.failUnlessEqual(response.status_code, 302)
        
        profiles = PlayerUserProfile.objects.all().order_by('-chess_points')
        self.failIf(profiles == None)
        self.failUnlessEqual(len(profiles), 12)

        self.failUnlessEqual(profiles[0].chess_points, 1217)
        self.failUnlessEqual(profiles[1].chess_points, 1200)
        self.failUnlessEqual(profiles[2].chess_points, 1200)
        self.failUnlessEqual(profiles[3].chess_points, 1200)
        self.failUnlessEqual(profiles[4].chess_points, 1200)
        self.failUnlessEqual(profiles[5].chess_points, 1200)
        self.failUnlessEqual(profiles[6].chess_points, 1200)
        self.failUnlessEqual(profiles[7].chess_points, 1200)
        self.failUnlessEqual(profiles[8].chess_points, 1200)
        self.failUnlessEqual(profiles[9].chess_points, 1200)
        self.failUnlessEqual(profiles[10].chess_points, 1199)
        self.failUnlessEqual(profiles[11].chess_points, 1184)

    def runTestChessUndo(self):
        c = Client()
        c.login(username='kokan', password='123')

        response = c.post("/mdcs/match/undo/2/")
        self.failIf(response == None)
        self.failUnlessEqual(response.status_code, 302)
        
        profiles = PlayerUserProfile.objects.all().order_by('-chess_points')
        self.failIf(profiles == None)
        self.failUnlessEqual(len(profiles), 12)

        self.failUnlessEqual(profiles[0].chess_points, 1216)
        self.failUnlessEqual(profiles[1].chess_points, 1200)
        self.failUnlessEqual(profiles[2].chess_points, 1200)
        self.failUnlessEqual(profiles[3].chess_points, 1200)
        self.failUnlessEqual(profiles[4].chess_points, 1200)
        self.failUnlessEqual(profiles[5].chess_points, 1200)
        self.failUnlessEqual(profiles[6].chess_points, 1200)
        self.failUnlessEqual(profiles[7].chess_points, 1200)
        self.failUnlessEqual(profiles[8].chess_points, 1200)
        self.failUnlessEqual(profiles[9].chess_points, 1200)
        self.failUnlessEqual(profiles[10].chess_points, 1200)
        self.failUnlessEqual(profiles[11].chess_points, 1184)

    def runTestTableFootballUndo(self):
        c = Client()
        c.login(username='kokan', password='123')

        profiles = PlayerUserProfile.objects.all().order_by('-tf_points')
        self.failIf(profiles == None)
        self.failUnlessEqual(len(profiles), 12)

        self.failUnlessEqual(profiles[0].tf_points, 1260)
        self.failUnlessEqual(profiles[1].tf_points, 1260)
        self.failUnlessEqual(profiles[2].tf_points, 1215)
        self.failUnlessEqual(profiles[3].tf_points, 1215)
        self.failUnlessEqual(profiles[4].tf_points, 1200)
        self.failUnlessEqual(profiles[5].tf_points, 1200)
        self.failUnlessEqual(profiles[6].tf_points, 1200)
        self.failUnlessEqual(profiles[7].tf_points, 1200)
        self.failUnlessEqual(profiles[8].tf_points, 1200)
        self.failUnlessEqual(profiles[9].tf_points, 1200)
        self.failUnlessEqual(profiles[10].tf_points, 1125)
        self.failUnlessEqual(profiles[11].tf_points, 1125)

        response = c.post("/mdcs/match/undo/6/")
        self.failIf(response == None)
        self.failUnlessEqual(response.status_code, 302)
        
        profiles = PlayerUserProfile.objects.all().order_by('-tf_points')
        self.failIf(profiles == None)
        self.failUnlessEqual(len(profiles), 12)

        self.failUnlessEqual(profiles[0].tf_points, 1260)
        self.failUnlessEqual(profiles[1].tf_points, 1260)
        self.failUnlessEqual(profiles[2].tf_points, 1200)
        self.failUnlessEqual(profiles[3].tf_points, 1200)
        self.failUnlessEqual(profiles[4].tf_points, 1200)
        self.failUnlessEqual(profiles[5].tf_points, 1200)
        self.failUnlessEqual(profiles[6].tf_points, 1200)
        self.failUnlessEqual(profiles[7].tf_points, 1200)
        self.failUnlessEqual(profiles[8].tf_points, 1200)
        self.failUnlessEqual(profiles[9].tf_points, 1200)
        self.failUnlessEqual(profiles[10].tf_points, 1140)
        self.failUnlessEqual(profiles[11].tf_points, 1140)